package com.zjj.edu.web.controller.system;

import com.zjj.edu.common.annotation.Log;
import com.zjj.edu.common.constant.UserConstants;
import com.zjj.edu.common.core.controller.BaseController;
import com.zjj.edu.common.core.domain.AjaxResult;
import com.zjj.edu.common.core.page.TableDataInfo;
import com.zjj.edu.common.core.text.Convert;
import com.zjj.edu.common.enums.BusinessType;
import com.zjj.edu.common.utils.poi.ExcelUtil;
import com.zjj.edu.framework.util.ShiroUtils;
import com.zjj.edu.framework.util.UserPermissionUtil;
import com.zjj.edu.system.domain.SysCampus;
import com.zjj.edu.system.domain.SysClassRoom;
import com.zjj.edu.system.domain.SysUser;
import com.zjj.edu.system.service.ISysCampusService;
import com.zjj.edu.system.service.ISysClassRoomService;
import com.zjj.edu.system.service.ISysTenantService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 教室Controller
 *
 * @author zjj
 * @date 2020-06-02
 */
@Controller
@RequestMapping("/system/classRoom")
public class SysClassRoomController extends BaseController
{
    private String prefix = "system/classRoom";

    @Autowired
    private ISysClassRoomService sysClassRoomService;

    @Autowired
    private ISysTenantService tenantService;

    @Autowired
    private ISysCampusService sysCampusService;

    @RequiresPermissions("system:classRoom:view")
    @GetMapping()
    public String classRoom(ModelMap mmap)
    {
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/classRoom";
    }

    /**
     * 查询教室列表
     */
    @RequiresPermissions("system:classRoom:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysClassRoom sysClassRoom)
    {
        startPage();
        sysClassRoom.setTenantId(ShiroUtils.getTenantId());
        List<SysClassRoom> list = sysClassRoomService.selectSysClassRoomList(sysClassRoom);
        return getDataTable(list);
    }

    /**
     * 导出教室列表
     */
    @RequiresPermissions("system:classRoom:export")
    @Log(title = "教室", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysClassRoom sysClassRoom)
    {
        sysClassRoom.setTenantId(ShiroUtils.getTenantId());
        List<SysClassRoom> list = sysClassRoomService.selectSysClassRoomList(sysClassRoom);
        ExcelUtil<SysClassRoom> util = new ExcelUtil<SysClassRoom>(SysClassRoom.class);
        return util.exportExcel(list, "classRoom");
    }

    /**
     * 新增教室
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/add";
    }

    /**
     * 新增保存教室
     */
    @RequiresPermissions("system:classRoom:add")
    @Log(title = "教室", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysClassRoom sysClassRoom)
    {
        sysClassRoom.setTenantId(ShiroUtils.getTenantId());
        if (UserConstants.NAME_NOT_UNIQUE.equals(sysClassRoomService.checkNameUnique(sysClassRoom))) {
            return error("新增教室'" + sysClassRoom.getName() + "'失败，名称已存在");
        }
        sysClassRoom.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(sysClassRoomService.insertSysClassRoom(sysClassRoom));
    }

    /**
     * 校验名称
     */
    @PostMapping("/checkNameUnique")
    @ResponseBody
    public String checkTenantNameUnique(SysClassRoom sysClassRoom) {
        return sysClassRoomService.checkNameUnique(sysClassRoom);
    }

    /**
     * 修改教室
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("sysClassRoom", sysClassRoomService.selectSysClassRoomById(id));
        mmap.put("tenantList", tenantService.selectTenantAll());

        SysCampus sysCampus = new SysCampus();
        sysCampus.setTenantId(ShiroUtils.getTenantId());
        List<SysCampus> campusList = sysCampusService.selectSysCampusList(sysCampus);
        mmap.put("campusList", campusList);
        return prefix + "/edit";
    }

    /**
     * 修改保存教室
     */
    @RequiresPermissions("system:classRoom:edit")
    @Log(title = "教室", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysClassRoom sysClassRoom)
    {
        sysClassRoom.setTenantId(ShiroUtils.getTenantId());
        if (UserConstants.NAME_NOT_UNIQUE.equals(sysClassRoomService.checkNameUnique(sysClassRoom))) {
            return error("修改教室'" + sysClassRoom.getName() + "'失败，名称已存在");
        }
        sysClassRoom.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(sysClassRoomService.updateSysClassRoom(sysClassRoom));
    }

    /**
     * 删除教室
     */
    @RequiresPermissions("system:classRoom:remove")
    @Log(title = "教室", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        SysUser user = ShiroUtils.getSysUser();
        SysClassRoom classRoom = new SysClassRoom();
        classRoom.setIds(Convert.toStrArray(ids));
        classRoom.setUpdateBy(user.getUserName());
        if (!UserPermissionUtil.checkUserPermiss()) {
            classRoom.setTenantId(user.getTenantId());
        }
        return toAjax(sysClassRoomService.deleteSysClassRoomByIds(classRoom));
    }

    /**
     * 状态修改
     */
    @Log(title = "教室管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:classRoom:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysClassRoom sysClassRoom)
    {
        sysClassRoom.setTenantId(ShiroUtils.getTenantId());
        return toAjax(sysClassRoomService.updateSysClassRoom(sysClassRoom));
    }

}
